This is an R Markdown Notebook. Each section of the code is then explained.
First of all import the libraries needed
#install.packages(c("datavolley", "ovlytics"))
library(datavolley)
library(ggplot2)
## Warning: il pacchetto 'ggplot2' è stato creato con R versione 4.3.2
library(dplyr)
## Warning: il pacchetto 'dplyr' è stato creato con R versione 4.3.2
##
## Caricamento pacchetto: 'dplyr'
## I seguenti oggetti sono mascherati da 'package:stats':
##
## filter, lag
## I seguenti oggetti sono mascherati da 'package:base':
##
## intersect, setdiff, setequal, union
library(ovlytics)
Import the file you are interested in considering more than one match, you have to import all the folder
filename <- "C:/Users/mirko/Documents/GitHub/CuneoWebsite.io/Assets/Cuneo-Bergamo_cuneo.dvw"
#d <- dir("C:/Users/mirko/OneDrive - Politecnico di Milano/Altro/Volley/Conco2324/Parella Torino/Ritorno/", pattern = "dvw$", full.names = TRUE)
teamName = 'HONDA OLIVERO S.BERNARDO CUNEO'
x <- dv_read(filename)
## Warning in stri_enc_detect2(tst): stri_enc_detect2 is deprecated and will be
## removed in a future release of 'stringi'.
serve_idx <- find_serves(plays(x))
table(plays(x)$team[serve_idx])
##
## HONDA OLIVERO S.BERNARDO CUNEO VOLLEY BERGAMO 1991
## 98 93
Funzioni utili
## find rows where a single player is on court
player_on_court <- function(x, target_player_id, team = NULL) {
if (!is.null(team)) team <- match.arg(team, c("home", "visiting"))
## 'team' is optional here, if NULL then we look at both home and visiting teams
idx <- rep(FALSE, nrow(x))
if (is.null(team) || team == "home") {
idx <- idx | x$home_player_id1 == target_player_id | x$home_player_id2 == target_player_id | x$home_player_id3 == target_player_id |
x$home_player_id4 == target_player_id | x$home_player_id5 == target_player_id | x$home_player_id6 == target_player_id
}
if (is.null(team) || team == "visiting") {
idx <- idx | x$visiting_player_id1 == target_player_id | x$visiting_player_id2 == target_player_id | x$visiting_player_id3 == target_player_id |
x$visiting_player_id4 == target_player_id | x$visiting_player_id5 == target_player_id | x$visiting_player_id6 == target_player_id
}
idx[is.na(idx)] <- FALSE
idx
}
## find rows where any of our target players are on court
any_player_on_court <- function(x, target_player_ids, team = NULL) {
## for each target player, find rows where they are on court
out <- lapply(target_player_ids, function(pid) player_on_court(x, target_player_id = pid, team = team))
## and now find rows where ANY of those players were on court
apply(do.call(cbind, out), 1, any)
}
## find rows where all of our target players are on court
all_players_on_court <- function(x, target_player_ids, team = NULL) {
## for each target player, find rows where they are on court
out <- lapply(target_player_ids, function(pid) player_on_court(x, target_player_id = pid, team = team))
## and now find rows where ALL of those players were on court
apply(do.call(cbind, out), 1, all)
}
d <- dir("C:/Users/mirko/Documents/GitHub/CuneoWebsite.io/Assets/", pattern = "dvw$", full.names = TRUE)
lx <- list()
## read each file
for (fi in seq_along(d)) lx[[fi]] <- dv_read(d[fi], insert_technical_timeouts = FALSE)
## Warning in stri_enc_detect2(tst): stri_enc_detect2 is deprecated and will be
## removed in a future release of 'stringi'.
## now extract the play-by-play component from each and bind them together
px <- list()
for (fi in seq_along(lx)) px[[fi]] <- plays(lx[[fi]])
px <- do.call(rbind, px)
Ora analizziamo la ricezione:
#, end_zone == 5
table_data <- px %>%
dplyr::filter(skill == "Reception", team == teamName) %>%
group_by(player_name) %>%
dplyr::summarize(
N_receptions = n(),
count_perfette = sum(evaluation_code == "#", na.rm = TRUE),
count_positive = sum(evaluation_code == "+", na.rm = TRUE),
count_escalamative = sum(evaluation_code == "!", na.rm = TRUE),
count_negative = sum(evaluation_code == "-", na.rm = TRUE),
count_errori = sum(evaluation_code == "=", na.rm = TRUE),
positività = (count_positive + count_perfette)/N_receptions,
efficienza = (count_positive + count_perfette - count_errori)/N_receptions,
)
table_data
library(plotly)
## Warning: il pacchetto 'plotly' è stato creato con R versione 4.3.2
##
## Caricamento pacchetto: 'plotly'
## Il seguente oggetto è mascherato da 'package:ggplot2':
##
## last_plot
## Il seguente oggetto è mascherato da 'package:stats':
##
## filter
## Il seguente oggetto è mascherato da 'package:graphics':
##
## layout
# Create bubble chart for each player
fig <- plot_ly(table_data,
x = ~positività,
y = ~efficienza,
type = 'scatter',
mode = 'markers',
hovertemplate = paste('<i>Player</i>: %{text}',
'<br><b>Ricezione</b>: %{marker.size}<extra></extra>'),
color = ~positività,
marker = list(size = ~N_receptions, sizemode = "area", sizeref = 0.005, opacity = 0.5),
text = ~player_name
)
fig <- fig %>% layout(title = 'Qualità Ricezione',
xaxis = list(title = 'Positività', showgrid = TRUE),
yaxis = list(title = 'Efficienza', showgrid = TRUE)
)
fig
# end_zone == 5
table_data <- px %>%
dplyr::filter(skill == "Attack", team == teamName) %>%
group_by(player_name) %>%
dplyr::summarize(
N_attacks = n(),
count_perfette = sum(evaluation_code == "#", na.rm = TRUE),
count_positive = sum(evaluation_code == "+", na.rm = TRUE),
count_escalamative = sum(evaluation_code == "!", na.rm = TRUE),
count_negative = sum(evaluation_code == "-", na.rm = TRUE),
count_errori = sum(evaluation_code == "=", na.rm = TRUE),
positività = (count_positive + count_perfette)/N_attacks,
efficienza = (count_positive + count_perfette - count_errori)/N_attacks,
)
table_data
fig <- plot_ly(table_data,
x = ~positività,
y = ~efficienza,
type = 'scatter',
mode = 'markers',
hovertemplate = paste('<i>Player</i>: %{text}',
'<br><b>Attacchi</b>: %{marker.size}<extra></extra>'),
color = ~positività,
marker = list(size = ~N_attacks, sizemode = "area", sizeref = 0.01, opacity = 0.5),
text = ~player_name
)
fig <- fig %>% layout(title = 'Qualità Attacco',
xaxis = list(title = 'Positività', showgrid = FALSE),
yaxis = list(title = 'Efficienza', showgrid = FALSE)
)
fig